
package cn.kgc.tangcco.tcbd1011.Reader.action;

import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.kgc.tangcco.tcbd1011.Reader.entity.ReaderInfo;
import cn.kgc.tangcco.tcbd1011.Reader.services.ReaderServices;
import cn.kgc.tangcco.tcbd1011.comm.base.BaseServlet;
import cn.kgc.tangcco.tcbd1011.comm.properties.PropertiesFactory;
import cn.kgc.tangcco.tcbd1011.util.MyBeanUtils;
import cn.kgc.tangcco.tcbd1011.util.Util;


@WebServlet(name="ReaderServlet",urlPatterns="/reader.action")
public class ReaderServlet extends BaseServlet {
	
	public static final String SUCCESS="reader.action?methodName=doListReader";
	
	private ReaderServices rser;
	public ReaderServlet() {
		rser=(ReaderServices) PropertiesFactory.getInstance(Util.READESERVICES);
	}
	
	public String doListReader(HttpServletRequest request, HttpServletResponse response) {
		String forward = "reader/listReader";
		ReaderInfo reInfo=new ReaderInfo();
		reInfo.setReadername(request.getParameter("readername"));
		request.setAttribute("rlist",this.rser.queryAllReader(MyBeanUtils.populate(ReaderInfo.class, request.getParameterMap())));
		return forward;
	}
	public String toAddReader(HttpServletRequest request, HttpServletResponse response) {
		String forward = "reader/addReader";
		return forward;
	}
	public String doAddReader(HttpServletRequest request, HttpServletResponse response) {
		String forward = SUCCESS;
		request.setAttribute("rows", this.rser.addReader(MyBeanUtils.populate(ReaderInfo.class, request.getParameterMap())));
		return forward;
	}
	
	public String detailReader(HttpServletRequest request, HttpServletResponse response) {
		String forward = "reader/detailReader";
		request.setAttribute("reade", this.rser.detailReader(Util.parseInt(request.getParameter("readerid"))));
		return forward;
	}
	
	public String toUpdateReader(HttpServletRequest request, HttpServletResponse response) {
		String forward = "reader/updateReader";
		request.setAttribute("reade", this.rser.detailReader(Util.parseInt(request.getParameter("readerid"))));
		return forward;
	}
	
	public String doUpdateReader(HttpServletRequest request, HttpServletResponse response) {
		String forward = SUCCESS;
		Map<String, String[]> param=request.getParameterMap();
		ReaderInfo  reader=MyBeanUtils.populate(ReaderInfo.class, param);
		int rows=this.rser.updateReader(reader);
		request.setAttribute("rows", rows);
		return forward;
	}
	
	public String doDeleteReader(HttpServletRequest request, HttpServletResponse response) {
		String forward = SUCCESS;
		int rows=this.rser.deleteReader(Util.parseInt(request.getParameter("readerid")));
		request.setAttribute("rows", rows);
		return forward;
	}
	

}
